/**
 * MVideoLive.java
 * 版权所有(C) 2014
 * 创建者:cuiran 2014-7-22 上午9:30:43
 */
package com.ghyf.mplay.view;

import java.util.ArrayList;
import java.util.Map;

import org.videolan.libvlc.EventHandler;
import org.videolan.libvlc.IVideoPlayer;
import org.videolan.libvlc.LibVLC;
import org.videolan.libvlc.LibVlcException;
import org.videolan.libvlc.LibVlcUtil;
import org.videolan.libvlc.Media;
import org.videolan.vlc.audio.AudioServiceController;
import org.videolan.vlc.util.VLCInstance;

import com.ghyf.mplay.WeakHandler;
import com.ghyf.mplay.application.CookieConstants;
import com.ghyf.mplay.application.UserInfoSingleton;
import com.ghyf.mplay.net.common.RecvBase;
import com.ghyf.mplay.util.LogUtil;


import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.graphics.Color;
import android.graphics.ImageFormat;
import android.graphics.PixelFormat;
import android.media.AudioManager;
import android.media.AudioManager.OnAudioFocusChangeListener;
import android.net.Uri;
import android.os.Handler;
import android.os.Message;
import android.preference.PreferenceManager;
import android.util.AttributeSet;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.SurfaceHolder.Callback;
import android.view.ViewGroup.LayoutParams;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.RotateAnimation;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;

/**
 * 直播视频使用的view 
 * @author cuiran
 * @version 1.0.0
 */
public class MVideoLive extends SurfaceView implements IVideoPlayer {
    public final static String TAG = "VLC/MVideoLive";

	
	private Context mContext;
    // Internal intent identifier to distinguish between internal launch and
    // external intent.
    private final static String PLAY_FROM_VIDEOGRID = "org.videolan.vlc.gui.video.PLAY_FROM_VIDEOGRID";

    private SurfaceView mSurface;
    private SurfaceView mSubtitlesSurface;
    private SurfaceHolder mSurfaceHolder;
    private SurfaceHolder mSubtitlesSurfaceHolder;
    private FrameLayout mSurfaceFrame;
   
    private LibVLC mLibVLC;
    private String mLocation;

    private static final int SURFACE_BEST_FIT = 0;
    private static final int SURFACE_FIT_HORIZONTAL = 1;
    private static final int SURFACE_FIT_VERTICAL = 2;
    private static final int SURFACE_FILL = 3;
    private static final int SURFACE_16_9 = 4;
    private static final int SURFACE_4_3 = 5;
    private static final int SURFACE_ORIGINAL = 6;
    private int mCurrentSize = SURFACE_BEST_FIT;

    /** Overlay */
    private View mOverlayHeader;
    private View mOverlayOption;
    private View mOverlayProgress;
    private View mOverlayBackground;
    private static final int OVERLAY_TIMEOUT = 4000;
    private static final int OVERLAY_INFINITE = 3600000;
    private static final int FADE_OUT = 1;
    private static final int SHOW_PROGRESS = 2;
    private static final int SURFACE_SIZE = 3;
    private static final int AUDIO_SERVICE_CONNECTION_SUCCESS = 5;
    private static final int AUDIO_SERVICE_CONNECTION_FAILED = 6;
    private static final int FADE_OUT_INFO = 4;
    private boolean mDragging;
    private boolean mShowing;
    private int mUiVisibility = -1;
    private SeekBar mSeekbar;
    private TextView mTitle;
    private TextView mSysTime;
    private TextView mBattery;
    private TextView mTime;
    private TextView mLength;
    private TextView mInfo;
    private ImageView mLoading;
    private TextView mLoadingText;
    private ImageButton mPlayPause;
    private ImageButton mBackward;
    private ImageButton mForward;
    private boolean mEnableJumpButtons;
    private boolean mEnableBrightnessGesture;
    private boolean mDisplayRemainingTime = false;
    private int mScreenOrientation;
    private ImageButton mAudioTrack;
    private ImageButton mSubtitle;
    private ImageButton mLock;
    private ImageButton mSize;
    private ImageButton mMenu;
    private boolean mIsLocked = false;
    private int mLastAudioTrack = -1;
    private int mLastSpuTrack = -2;

    /**
     * For uninterrupted switching between audio and video mode
     */
    private boolean mSwitchingView;
    private boolean mEndReached;
    private boolean mCanSeek;

    // Playlist
    private int savedIndexPosition = -1;

    // size of the video
    private int mVideoHeight;
    private int mVideoWidth;
    private int mVideoVisibleHeight;
    private int mVideoVisibleWidth;
    private int mSarNum;
    private int mSarDen;

    //Volume
    private AudioManager mAudioManager;
    private int mAudioMax;
    private OnAudioFocusChangeListener mAudioFocusListener;

    //Touch Events
    private static final int TOUCH_NONE = 0;
    private static final int TOUCH_VOLUME = 1;
    private static final int TOUCH_BRIGHTNESS = 2;
    private static final int TOUCH_SEEK = 3;
    private int mTouchAction;
    private int mSurfaceYDisplayRange;
    private float mTouchY, mTouchX, mVol;

    // Brightness
    private boolean mIsFirstBrightnessGesture = true;

    // Tracks & Subtitles
    private Map<Integer,String> mAudioTracksList;
    private Map<Integer,String> mSubtitleTracksList;
    /**
     * Used to store a selected subtitle; see onActivityResult.
     * It is possible to have multiple custom subs in one session
     * (just like desktop VLC allows you as well.)
     */
    private final ArrayList<String> mSubtitleSelectedFiles = new ArrayList<String>();

    // Whether fallback from HW acceleration to SW decoding was done.
    private boolean mDisabledHardwareAcceleration = false;
    private int mPreviousHardwareAccelerationMode;
    
    private static MVideoLive instance =null;
    
    

    public static MVideoLive getInstance(Context context){
    	if(instance ==null){
    		instance=new MVideoLive(context);
    	}
    	return instance;
    }
	/**
	 * @param context
	 */
	public MVideoLive(Context context) {
		super(context);
//		initVideoView(context);
		this.mContext=context;
	}
	
	/**
	 * @param context
	 * @param attrs
	 */
	public MVideoLive(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
//		initVideoView(context);
	}
	
	/**
	 * @param context
	 * @param attrs
	 * @param defStyle
	 */
	public MVideoLive(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		// TODO Auto-generated constructor stub
//		initVideoView(context);
	}
	SurfaceHolder surfaceHolder=null;
	/**
	 * init VideoView
	 * 2014-6-23 下午6:15:49
	 * @param ctx
	 *
	 */
	 public void initVideoView() {
		 
		    SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(mContext);
		     surfaceHolder= getHolder();
		    String chroma = pref.getString("chroma_format", "");
	        if(LibVlcUtil.isGingerbreadOrLater() && chroma.equals("YV12")) {
	        	surfaceHolder.setFormat(ImageFormat.YV12);
	        } else if (chroma.equals("RV16")) {
	        	surfaceHolder.setFormat(PixelFormat.RGB_565);
	        } else {
	        	surfaceHolder.setFormat(PixelFormat.RGBX_8888);
	        }
	        surfaceHolder.addCallback(mSurfaceCallback);
	        mSurface=this;
		    setFocusable(true);
		    setFocusableInTouchMode(true);
		    requestFocus();
		     /* Services and miscellaneous */
	        mAudioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
	        mAudioMax = mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
	        
	        /* Loading view */
	//        LayoutInflater factory = LayoutInflater.from(mContext);
	//		//得到自定义对话框
	//		View loadView = factory.inflate(R.layout.loading, null);
		
	//        mLoading = (ImageView)loadView.findViewById(R.id.player_overlay_loading);
	//        mLoadingText = (TextView)loadView.findViewById(R.id.player_overlay_loading_text);
	//        TipsUtil.alertCustomDialog(mContext, loadView);
	        
	//        mLoading=new ImageView(mContext);
	//        mLoadingText=new TextView(mContext);
	        // Signal to LibVLC that the videoPlayerActivity was created, thus the
	        // SurfaceView is now available for MediaCodec direct rendering.
	      
	        EventHandler em = EventHandler.getInstance();
	        em.addHandler(eventHandler);
	        
	      
		    if (mContext instanceof Activity)
		      ((Activity) mContext).setVolumeControlStream(AudioManager.STREAM_MUSIC);
		    
		    try {
	            mLibVLC = VLCInstance.getLibVlcInstance();
	        } catch (LibVlcException e) {
	            LogUtil.i(TAG, "LibVLC initialisation failed");
	            return;
	        }
		    mLibVLC.eventVideoPlayerActivityCreated(true);
		    LogUtil.i(TAG, "Hardware acceleration mode: "+mLibVLC.getHardwareAcceleration());
	 }
	 
	 /**
	  * 当控件显示时自动执行
	  */
	@Override
	protected void onAttachedToWindow() {
		LogUtil.i(TAG, "LibVLC onAttachedToWindow ");
		
		super.onAttachedToWindow();
	}
	/**
	 * 当控件从界面退出时自动执行
	 */
	@Override
	protected void onDetachedFromWindow() {
		LogUtil.i(TAG, "LibVLC onDetachedFromWindow ");

        super.onDetachedFromWindow();
	}

	private ProgressDialog mPD;
 
	private void startLoading() {

		/**
		 * 此时可以显示一个进度条
		 */
		this.setBackgroundColor(Color.BLACK);
		mPD = new ProgressDialog(mContext);
	    mPD.setCancelable(false);
	    mPD.setMessage("正在加载...");
//	    Window window=mPD.getWindow();
	    mPD.show();
	 
  }
  
  private void stopLoading(){
		if(mPD!=null){
			mPD.dismiss();
		}
		this.setBackgroundColor(Color.TRANSPARENT);
	
  }
  private void sendStart(){
	  boolean isRefresh=UserInfoSingleton.getBoolean(CookieConstants.IsRefresh,false);
	  if(!isRefresh){
		  Intent mIntent = new Intent(RecvBase.ACTION_CMD_CONTROL);
		  mIntent.putExtra("cmd",RecvBase.CMD_CONTROL_PLAYLOCAL_1);
		  //发送广播
		  mContext.sendBroadcast(mIntent);
	  }else{
		  UserInfoSingleton.putBooleanAndCommit(CookieConstants.IsRefresh, false);
	  }
	  
	
  }
  private void sendStop(){
	  Intent mIntent = new Intent(RecvBase.ACTION_CMD_CONTROL);
	  mIntent.putExtra("cmd",(short)100);
	  //发送广播
	  mContext.sendBroadcast(mIntent);
  }
  /**
     * Start the video loading animation.
     */
    private void startLoadingAnimation() {
        AnimationSet anim = new AnimationSet(true);
        RotateAnimation rotate = new RotateAnimation(0f, 360f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
        rotate.setDuration(800);
        rotate.setInterpolator(new DecelerateInterpolator());
        rotate.setRepeatCount(RotateAnimation.INFINITE);
        anim.addAnimation(rotate);
        mLoading.startAnimation(anim);
        mLoadingText.setVisibility(View.VISIBLE);
    }

    /**
     * Stop the video loading animation.
     */
    private void stopLoadingAnimation() {
        mLoading.setVisibility(View.INVISIBLE);
        mLoading.clearAnimation();
        mLoadingText.setVisibility(View.GONE);
//        TipsUtil.closeCustomDialog();
    }
 
 public void bindAudioService(){
	 AudioServiceController.getInstance().bindAudioService(mContext,
                new AudioServiceController.AudioServiceConnectionListener() {
            @Override
            public void onConnectionSuccess() {
                mHandler.sendEmptyMessage(AUDIO_SERVICE_CONNECTION_SUCCESS);
            }

            @Override
            public void onConnectionFailed() {
                mHandler.sendEmptyMessage(AUDIO_SERVICE_CONNECTION_FAILED);
            }
        });
 }
   /**
 *
 */
 public void play() {
    mLibVLC.play();
    bindAudioService();
    mSurface.setKeepScreenOn(true);
}
 

/**
 *
 */
public void pause() {
    mLibVLC.pause();
    mSurface.setKeepScreenOn(false);
}
 
public void stop() {
	LogUtil.i(TAG, "----------onPause");
   
    long time = mLibVLC.getTime();
    long length = mLibVLC.getLength();
    //remove saved position if in the last 5 seconds
    if (length - time < 5000)
        time = 0;
    else
        time -= 5000; // go back 5 seconds, to compensate loading time

    /*
     * Pausing here generates errors because the vout is constantly
     * trying to refresh itself every 80ms while the surface is not
     * accessible anymore.
     * To workaround that, we keep the last known position in the playlist
     * in savedIndexPosition to be able to restore it during onResume().
     */
    mLibVLC.stop();
	mSurface.setKeepScreenOn(false);
	AudioServiceController.getInstance().unbindAudioService(mContext);
	
}
/**
 *
 */
public void onPause() {
	LogUtil.i(TAG, "----------onPause----------");

      if(mSwitchingView) {
          Log.d(TAG, "mLocation = \"" + mLocation + "\"");
          AudioServiceController.getInstance().showWithoutParse(savedIndexPosition);
          AudioServiceController.getInstance().unbindAudioService(mContext);
          return;
      }

      long time = mLibVLC.getTime();
      long length = mLibVLC.getLength();
      //remove saved position if in the last 5 seconds
      if (length - time < 5000)
          time = 0;
      else
          time -= 5000; // go back 5 seconds, to compensate loading time

      /*
       * Pausing here generates errors because the vout is constantly
       * trying to refresh itself every 80ms while the surface is not
       * accessible anymore.
       * To workaround that, we keep the last known position in the playlist
       * in savedIndexPosition to be able to restore it during onResume().
       */
      mLibVLC.stop();
      mSurface.setKeepScreenOn(false);
      
      AudioServiceController.getInstance().unbindAudioService(mContext);
	
}

public void onStop() {
 
    LogUtil.i(TAG, "----------onStop----------");

}

public void onDestroy() {
   
	LogUtil.i(TAG, "----------onDestroy----------");
    EventHandler em = EventHandler.getInstance();
    em.removeHandler(eventHandler);

    // MediaCodec opaque direct rendering should not be used anymore since there is no surface to attach.
    mLibVLC.eventVideoPlayerActivityCreated(false);
    // HW acceleration was temporarily disabled because of an error, restore the previous value.
    if (mDisabledHardwareAcceleration)
        mLibVLC.setHardwareAcceleration(mPreviousHardwareAccelerationMode);

    mAudioManager = null;
}

public void destroy() {
	LogUtil.i(TAG, "vlc---destroy");
	EventHandler em = EventHandler.getInstance();
    em.removeHandler(eventHandler);
    // MediaCodec opaque direct rendering should not be used anymore since there is no surface to attach.
    mLibVLC.eventVideoPlayerActivityCreated(false);
    // HW acceleration was temporarily disabled because of an error, restore the previous value.
    if (mDisabledHardwareAcceleration)
        mLibVLC.setHardwareAcceleration(mPreviousHardwareAccelerationMode);

    mAudioManager = null;
}
 public void setVideoPath(String path) {
	 mLocation=LibVLC.PathToURI(path);
	 if (mLocation != null && mLocation.length() > 0){
		  AudioServiceController.getInstance().stop(); // Stop the previous playback.
		  mLibVLC.setMediaList();
		  mLibVLC.getMediaList().add(new Media(mLibVLC, mLocation));
		  savedIndexPosition = mLibVLC.getMediaList().size() - 1;
		  mLibVLC.setHardwareAcceleration(LibVLC.HW_ACCELERATION_FULL);
		  mLibVLC.playIndex(savedIndexPosition);
//		  startLoading();
	 }
 }
 public void setVideoURI(Uri uri) {
	 
 }
 
 public void setVolume(int volume){
	 mLibVLC.setVolume(volume);
 }
 public int getVolume(){
	 return mLibVLC.getVolume();
 }
  /**
     *  Handle libvlc asynchronous events
     */
 private final Handler eventHandler = new MVideoLiveEventHandler(this);
 
 private static class MVideoLiveEventHandler extends WeakHandler<MVideoLive> {
        public MVideoLiveEventHandler(MVideoLive owner) {
            super(owner);
        }
        @Override
        public void handleMessage(Message msg) {
        	MVideoLive activity = getOwner();
            if(activity == null) return;
            // Do not handle events if we are leaving the VideoPlayerActivity
            if (activity.mSwitchingView) return;

            switch (msg.getData().getInt("event")) {
                case EventHandler.MediaParsedChanged:
                    LogUtil.i(TAG, "MediaParsedChanged");
                    if (activity.mLibVLC.getVideoTracksCount() < 1) {
                        LogUtil.i(TAG, "No video track, open in audio mode");
//                        activity.switchToAudioMode();
                    }
                    break;
                case EventHandler.MediaPlayerPlaying:
                    LogUtil.i(TAG, "MediaPlayerPlaying");
//                    activity.stopLoadingAnimation();
                    /**
                     * 直播开始时候，停止本地播放
                     */
                    activity.sendStop();
//                    activity.stopLoading();
                    break;
                case EventHandler.MediaPlayerPaused:
                    LogUtil.i(TAG, "MediaPlayerPaused");
                    break;
                case EventHandler.MediaPlayerStopped:
                    LogUtil.i(TAG, "MediaPlayerStopped");
                    /**
                     * 直播停止时候，切换本地播放
                     */
                    activity.sendStart();
                    break;
                case EventHandler.MediaPlayerEndReached:
                    LogUtil.i(TAG, "MediaPlayerEndReached");
                    activity.endReached();
                    break;
                case EventHandler.MediaPlayerVout:
//                    activity.handleVout(msg);
                    break;
                case EventHandler.MediaPlayerPositionChanged:
                    if (!activity.mCanSeek)
                        activity.mCanSeek = true;
                    //don't spam the logs
                    break;
                case EventHandler.MediaPlayerEncounteredError:
                    LogUtil.i(TAG, "MediaPlayerEncounteredError");
//                    activity.encounteredError();
                    break;
                case EventHandler.HardwareAccelerationError:
                    LogUtil.i(TAG, "HardwareAccelerationError");
                 
                    break;
                case EventHandler.MediaPlayerTimeChanged:
                    // avoid useless error logs
                    break;
                default:
                    LogUtil.e(TAG, String.format("Event not handled (0x%x)", msg.getData().getInt("event")));
                    break;
            }
           
        }
        
 }
 
 

 	/**
     * Handle resize of the surface and the overlay
     */
 private final Handler mHandler = new MVideoLiveHandler(this);
    
  private static class MVideoLiveHandler extends WeakHandler<MVideoLive> {
       
	  public MVideoLiveHandler(MVideoLive owner) {
            super(owner);
      
	  }
        
	  @Override
        public void handleMessage(Message msg) {
		  MVideoLive activity = getOwner();
            if(activity == null) // WeakReference could be GC'ed early
                return;

            switch (msg.what) {
               
                case SURFACE_SIZE:
                    activity.changeSurfaceSize();
                    break;
              
            }
        }
  }

@Override
public void setSurfaceSize(int width, int height, int visible_width,
		int visible_height, int sar_num, int sar_den) {
	// TODO Auto-generated method stub
	 if (width * height == 0)
            return;

        // store video size
        mVideoHeight = height;
        mVideoWidth = width;
        mVideoVisibleHeight = visible_height;
        mVideoVisibleWidth  = visible_width;
        mSarNum = sar_num;
        mSarDen = sar_den;
        Message msg = mHandler.obtainMessage(SURFACE_SIZE);
        mHandler.sendMessage(msg);
}

 /* (non-Javadoc)
 * @see android.view.SurfaceView#onMeasure(int, int)
 */
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
	 LogUtil.i(TAG, "onMeasure >>>>>>>>>>>>>>>>>>>>>>>");
	int width = getDefaultSize(0, widthMeasureSpec); 
    int height = getDefaultSize(0, heightMeasureSpec); 
    
    setMeasuredDimension(width, height); 
}

private void endReached() {
    if(true) {
        LogUtil.i(TAG, "Found a video playlist, expanding it");
        eventHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
            	setVideoPath("/sata/media/jiuzai.mpg");
            }
        }, 1000);
    } else {
        /* Exit player when reaching the end */
        mEndReached = true;
        
    }
}


private void changeSurfaceSize() {
        int sw;
        int sh;

        // get screen size
            sw = ((Activity)mContext).getWindow().getDecorView().getWidth();
            sh = ((Activity)mContext).getWindow().getDecorView().getHeight();
     
        double dw = sw, dh = sh;
        boolean isPortrait;

        // getWindow().getDecorView() doesn't always take orientation into account, we have to correct the values
         isPortrait = getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT;
     
         LogUtil.e(TAG, "dw="+dw+",dh="+dh);
        if (sw > sh && isPortrait || sw < sh && !isPortrait) {
            dw = sh;
            dh = sw;
        }

        // sanity check
        if (dw * dh == 0 || mVideoWidth * mVideoHeight == 0) {
            LogUtil.e(TAG, "Invalid surface size");
            return;
        }

        // compute the aspect ratio
        double ar, vw;
        if (mSarDen == mSarNum) {
            /* No indication about the density, assuming 1:1 */
            vw = mVideoVisibleWidth;
            ar = (double)mVideoVisibleWidth / (double)mVideoVisibleHeight;
        } else {
            /* Use the specified aspect ratio */
            vw = mVideoVisibleWidth * (double)mSarNum / mSarDen;
            ar = vw / mVideoVisibleHeight;
        }

        // compute the display aspect ratio
        double dar = dw / dh;

        switch (mCurrentSize) {
            case SURFACE_BEST_FIT:
                if (dar < ar)
                    dh = dw / ar;
                else
                    dw = dh * ar;
                break;
            case SURFACE_FIT_HORIZONTAL:
                dh = dw / ar;
                break;
            case SURFACE_FIT_VERTICAL:
                dw = dh * ar;
                break;
            case SURFACE_FILL:
                break;
            case SURFACE_16_9:
                ar = 16.0 / 9.0;
                if (dar < ar)
                    dh = dw / ar;
                else
                    dw = dh * ar;
                break;
            case SURFACE_4_3:
                ar = 4.0 / 3.0;
                if (dar < ar)
                    dh = dw / ar;
                else
                    dw = dh * ar;
                break;
            case SURFACE_ORIGINAL:
                dh = mVideoVisibleHeight;
                dw = vw;
                break;
        }

        SurfaceView surface;
        SurfaceHolder surfaceHolder;
        surface = mSurface;
        surfaceHolder = mSurfaceHolder;
           
        // force surface buffer size
        surfaceHolder.setFixedSize(mVideoWidth, mVideoHeight);
    
        // set display size
        LayoutParams lp = surface.getLayoutParams();
        lp.width  = (int) Math.ceil(dw * mVideoWidth / mVideoVisibleWidth);
        lp.height = (int) Math.ceil(dh * mVideoHeight / mVideoVisibleHeight);
        lp.width  = this.getWidth();
        lp.height = this.getHeight();
        LogUtil.e(TAG, "width="+lp.width +",height="+lp.height);
        
        LogUtil.e(TAG, "width1="+this.getWidth() +",height2="+this.getHeight());
        surface.setLayoutParams(lp);
        surface.invalidate();
    }
  /**
 * attach and disattach surface to the lib
 */
private final SurfaceHolder.Callback mSurfaceCallback = new Callback() {
    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        if(format == PixelFormat.RGBX_8888)
            LogUtil.i(TAG, "Pixel format is RGBX_8888");
        else if(format == PixelFormat.RGB_565)
            LogUtil.i(TAG, "Pixel format is RGB_565");
        else if(format == ImageFormat.YV12)
            LogUtil.i(TAG, "Pixel format is YV12");
        else
            LogUtil.i(TAG, "Pixel format is other/unknown");
        mLibVLC.attachSurface(holder.getSurface(),MVideoLive.this);
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
    	 LogUtil.i(TAG, "----------surfaceCreated");
    	mSurfaceHolder = holder;
    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
    	LogUtil.i(TAG, "----------surfaceDestroyed");
        mLibVLC.detachSurface();
    }
};




}